﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class index : System.Web.UI.Page
{
	string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["xxnewsConnectionString"].ConnectionString;
	SqlConnection sqlcon;
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			//绑定主卡余额
			string maincardsql = "SELECT top(1) id, changetime, amount, balance, addname, addtime, addip FROM  youkaguanli_maincard_detail order by changetime desc,addtime desc,id desc";
			sqlcon = new SqlConnection(strCon);
			SqlDataAdapter myda = new SqlDataAdapter(maincardsql, sqlcon);
			DataSet myds = new DataSet();
			myda.Fill(myds, "maincardsql");
			decimal maincardbalance = (decimal)myds.Tables[0].Rows[0]["balance"];
			this.lblMainCardBalance.Text = Server.HtmlEncode(string.Format("{0:C}", maincardbalance).Replace("¥", "¥ "));
			this.lblTip.Visible = maincardbalance < 10000;
			this.lblMainCardBalance.ForeColor = maincardbalance > 10000 ? System.Drawing.Color.DarkGreen : (maincardbalance > 0 ? System.Drawing.Color.Chocolate : System.Drawing.Color.Red);
			this.lblTip.ForeColor = this.lblMainCardBalance.ForeColor;
			GridView1.Attributes.Add("bordercolor", "#bbddff");
			g_bind();
		}
	}
	protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D1DDF1'");
			e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
		}
		if (e.Row.RowIndex != -1)
		{
			int id = e.Row.RowIndex + 1 + GridView1.PageIndex * GridView1.PageSize;
			e.Row.Cells[1].Text = id.ToString();
			decimal money = (decimal)this.GridView1.DataKeys[e.Row.RowIndex]["balance"];
			((DataControlFieldCell)e.Row.Cells[8]).ForeColor = money > 500 ? System.Drawing.Color.DarkGreen : (money > 0 ? System.Drawing.Color.Chocolate : System.Drawing.Color.Red);
		}
	}
	public void g_bind()
	{
		Label_tiaojian.Text = "当前选择条件:";
		string countsql = @"select isnull(sum(a),0) as a,isnull(sum(b),0) as b,isnull(sum(c),0) as c,isnull(sum(balance),0) as balance from({0}) as t";
        string sqlstr = "select * from (select case when balance >500 then 1 else 0 end as a,case when balance> 0 and balance <= 500 then 1 else 0 end as b,case when balance<= 0 then 1 else 0 end as c,* from youkaguanli_card)as t1 where enabled=1 and isshowinmain=1 ";
		if (!string.IsNullOrEmpty(this.ddlDW0.SelectedValue.Trim()))
		{
			sqlstr += " and (jgname='" + this.ddlDW0.SelectedItem.Text.Trim() + "' or bzname='" + this.ddlDW0.SelectedItem.Text.Trim() + "') ";
			Label_tiaojian.Text += "单位：'" + this.ddlDW0.SelectedItem.Text + "及下级'；  ";
		}
		if (!string.IsNullOrEmpty(this.txtCardNum0.Text.Trim()))
		{
			sqlstr += " and cardnum like '%" + this.txtCardNum0.Text.Trim() + "%' ";
			Label_tiaojian.Text += "卡号：包含'" + this.txtCardNum0.Text.Trim() + "'；  ";
		}
		if (!string.IsNullOrEmpty(this.txtCarNum0.Text.Trim()))
		{
			sqlstr += " and carnum like '%" + this.txtCarNum0.Text.Trim() + "%' ";
			Label_tiaojian.Text += "车号：包含'" + this.txtCarNum0.Text.Trim() + "'；  ";
		}
		if (!string.IsNullOrEmpty(this.txtOwner0.Text.Trim()))
		{
			sqlstr += " and owner like '%" + this.txtOwner0.Text.Trim() + "%' ";
			Label_tiaojian.Text += "持卡人：包含'" + this.txtOwner0.Text.Trim() + "'；  ";
		}
		if (!(this.cbMoney0.Checked == this.cbMoney1.Checked && this.cbMoney1.Checked == this.cbMoney2.Checked))
		{
			//如果三个复选框的状态一样，意味着所有状态都查询。
			sqlstr += " and (";
			if (this.cbMoney0.Checked) { sqlstr += " a=1 or"; Label_tiaojian.Text += "资金" + this.cbMoney0.Text + "、"; }
			if (this.cbMoney1.Checked) { sqlstr += " b=1 or"; Label_tiaojian.Text += "资金" + this.cbMoney1.Text + "、"; }
			if (this.cbMoney2.Checked) { sqlstr += " c=1 or"; Label_tiaojian.Text += "资金" + this.cbMoney2.Text + "、"; }
			sqlstr = sqlstr.TrimEnd("or".ToCharArray());
            sqlstr += ")";
			Label_tiaojian.Text = Label_tiaojian.Text.TrimEnd('、') + "；  ";
        }
		countsql = string.Format(countsql, sqlstr);
		sqlstr += "  order by [enabled] DESC,[ordernum], [jgname], [bzname] ";
		if (Label_tiaojian.Text == "当前选择条件:") Label_tiaojian.Text = "当前选择条件:无";
		GridView1.PagerStyle.Font.Size = 10;
		dsAllCard.SelectCommand = sqlstr;
		GridView1.DataSourceID = dsAllCard.ID;
		GridView1.DataBind();
		try
		{
			sqlcon = new SqlConnection(strCon);
			SqlDataAdapter myda1 = new SqlDataAdapter(countsql, sqlcon);
			DataSet myds1 = new DataSet();
			myda1.Fill(myds1, "count");
			if (myds1.Tables[0].Rows.Count != 0)
			{
				label_zongjia.Visible = true;
				label_zongjia.Text = "当前选择条件下油卡的合计总金额为：" + Server.HtmlEncode( string.Format("{0:C}",(decimal)myds1.Tables[0].Rows[0]["balance"])) + "元，其中："
					+ "资金充足共" + myds1.Tables[0].Rows[0]["a"].ToString() + "张，"
					+ "资金紧张共" + myds1.Tables[0].Rows[0]["b"].ToString() + "张，"
					+ "资金欠缺共" + myds1.Tables[0].Rows[0]["c"].ToString() + "张。";
			}
			else
			{
				label_zongjia.Visible = false;
			}

		}
		catch (SqlException)
		{
			throw;
		}
		//label_zongjia.Visible = false;
	}
	protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
	{
		g_bind();
	}
	protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
	{
		this.ddlDW0.SelectedValue = string.Empty;
		this.txtCardNum0.Text = string.Empty;
		this.txtCarNum0.Text = string.Empty;
		this.txtOwner0.Text = string.Empty;
		this.cbMoney0.Checked = false;
		this.cbMoney1.Checked = false;
		this.cbMoney2.Checked = false;
		g_bind();
	}
	protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
	{
		g_bind();
		ImageButton1.Focus();
	}
	protected void GridView1_DataBound(object sender, EventArgs e)
	{
		GridViewRow btrow = GridView1.BottomPagerRow;
		Label lb1 = new Label();
		lb1.Text = "当前页码(" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + ")";
		if (btrow != null)
		{
			btrow.Cells[0].Controls.Add(lb1);
		}
	}
	protected void GridView1_PageIndexChanged(object sender, EventArgs e)
	{
	}
}
